/*
 * @Author: your name
 * @Date: 2021-09-19 16:18:01
 * @LastEditTime: 2021-09-21 16:49:24
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \Briup\JS\Swiper\lunbo-02\js\index.js
 */

//arrow_l 左边箭头    arrow_r 右边箭头   focus  主面板
var arrow_l = document.querySelector('.arrow-l');
var arrow_r = document.querySelector('.arrow-r');
var focus = document.querySelector('.focus');
// console.log(focus);
//定时器
var timer = null;

//给focus添加监视器，鼠标经过，就显示左右按钮，停止计时器
focus.addEventListener('mouseenter', function() {
    arrow_l.style.display = 'block';
    arrow_r.style.display = 'block';
    clearInterval(timer); //停止定时器
    timer = null;
});

//鼠标离开的事件
focus.addEventListener('mouseleave', function() {
    arrow_l.style.display = 'none';
    arrow_r.style.display = 'none';
    // console.log('b');
    // 手动调用定时器
    timer = setInterval(function() {
        // 手动调用点击事件
        arrow_r.click();
    }, 2000);
})

//ul是轮播图  ol是轮播图下方小圆圈
var ul = focus.querySelector('ul');
var ol = focus.querySelector('.circle');

//根据轮播图数量动态创建小圆圈的数量 且绑定事件
for (var i = 0; i < ul.children.length; i++) {
    var li = document.createElement('li');
    li.setAttribute('index', i);
    ol.appendChild(li);
    li.addEventListener('click', function() {
        //通过给小圆圈添加类名设置其样式
        for (var i = 0; i < ol.children.length; i++) {
            ol.children[i].className = '';
        }
        //点击的小圆圈设置类名为current
        this.className = 'current';
        var index = this.getAttribute('index');
        num = index;
        picChange(num); //小圆圈点击的时候也调用图片切换函数
    });
}

//设置初始态
ol.children[0].className = 'current';
var num = 0;

// 右边箭头添加事件
arrow_r.addEventListener('click', function() {
    num++;
    if (num > ul.children.length - 1) {
        num = 0;
    }
    picChange(num);
    circleChange(num);
    // console.log('右击');
});
//左边箭头添加事件
arrow_l.addEventListener('click', function() {
    num--;
    if (num < 0) {
        num = ul.children.length - 1;
    }
    picChange(num);
    circleChange(num);
    // console.log('左击');
})

// 小圆圈改变
function circleChange(circle) {
    for (var i = 0; i < ol.children.length; i++) {
        ol.children[i].className = '';
    }
    ol.children[circle].className = 'current';
}

// 图片切换
function picChange(num) {
    for (var i = 0; i < ul.children.length; i++) {
        ul.children[i].style.display = 'none';
    }
    ul.children[num].style.display = 'block';
}